package de.dopichaj.labrador.index;


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import de.dopichaj.labrador.index.meta.BaselineDocumentMetaDataCompressedIOFactory;
import de.dopichaj.labrador.index.meta.BaselineDocumentMetaDataFactory;
import de.dopichaj.labrador.index.meta.BaselineDocumentMetaDataIOFactory;
import de.dopichaj.labrador.index.meta.BaselineDocumentMetaDataVLIIOFactory;
import de.dopichaj.labrador.index.meta.DocumentDigest;
import de.dopichaj.labrador.index.meta.HitFactory;
import de.dopichaj.labrador.search.NEXISearcher;
import de.dopichaj.labrador.util.Configuration;
import de.dopichaj.labrador.util.io.ByteReader;


/**
 * An index.
 * Concrete subclasses should override the abstract methods in the
 * obvious manner.
 * 
 * <p>For all methods that return a closable resource, the caller is 
 * responsible for closing the resource.</p>
 */
public abstract class Index {

    private final Tokenizer tokenizer;
    private final Configuration configuration;

    /**
     * Construct an index with the given list of stopwords (subclasses are
     * responsible for storing and obtaining this list).
     */
    protected Index(final String[] stopWords, final Configuration configuration) {
        
        this.tokenizer = new Tokenizer(stopWords);
        this.configuration = configuration;
    }

    /**
     * Check if the index is dirty.
     * @see #markAsClean() #markAsDirty()
     */
    public abstract boolean isDirty();

    /**
     * Mark the index as dirty.
     * @throws FileNotFoundException  if the dirty marker cannot be established.
     */
    public abstract void markAsDirty() throws FileNotFoundException;

    /**
     * Mark the index as clean.
     */
    public abstract void markAsClean();

    /**
     * Get a reader for the tag ID file.
     * @throws FileNotFoundException
     */
    public abstract BufferedReader getTagIdReader()
        throws FileNotFoundException;

    /**
     * Get the tag factory for this index. Note that this method reads the
     * data from the index, so you should cache the value if possible.
     * @throws IOException
     */
    public TagFactory getTagFactory() throws IOException {
        TagFactory tagFactory = new TagFactory();
        BufferedReader reader = getTagIdReader();
        try {
            tagFactory.read(reader);
        } finally {
            reader.close();
        }
        return tagFactory;
    }
    /**
     * Get a PrintWriter for the tag ID file.
     * @throws FileNotFoundException
     */
    public abstract PrintWriter getTagIdWriter()
        throws FileNotFoundException;

    /**
     * Get the document digest byte reader. This reader contains a
     * sequence of document digests; from this stream, {@link DocumentDigest}s
     * can be constructed.
     * @throws FileNotFoundException
     */
    public abstract ByteReader getDigestByteReader()
        throws FileNotFoundException;

    /**
     * Get the document digest output stream.
     * @throws FileNotFoundException
     * @see #getDigestInputStream()
     */
    public abstract OutputStream getDigestOutputStream()
        throws FileNotFoundException;

    /**
     * Get the tokenizer for this index.
     */
    public Tokenizer getTokenizer() {
        return tokenizer;
    }
    
    public abstract void storeMetaData(int documentCount, int elementCount,
        long elementLengthSum) throws IOException;
    public abstract int getDocumentCount();
    public abstract int getElementCount();
    public abstract long getElementLengthSum();
    
    public abstract NEXISearcher getNEXISearcher() throws IOException;
    
    /**
     * Get the HitFactory.
     * @throws IOException
     */
    public HitFactory getHitFactory() throws IOException {
    
        final ByteReader input = getDigestByteReader();
        final HitFactory map = new HitFactory(getDocumentMetaDataFactory(),
            input, getTagFactory());
        return map;
    }

    public BaselineDocumentMetaDataFactory getDocumentMetaDataFactory() {
        return new BaselineDocumentMetaDataFactory(getIOFactory());
    }

    public abstract WritableIndex getWritableIndex() throws IOException;
    
    public Configuration getConfiguration() {
        return configuration;
    }

    public BaselineDocumentMetaDataIOFactory getIOFactory() {
        if (getConfiguration().getString("metadata-io", "vli").equals("compressed")) {
            return new BaselineDocumentMetaDataCompressedIOFactory();
        } else {
            return new BaselineDocumentMetaDataVLIIOFactory();
        }
    }
}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/